# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _

import datetime



import logging
_logger = logging.getLogger(__name__)

class CrEmpPromise(models.Model):

    _name = "cr.emp.promise"
    _description = "承诺"

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')
    pledge_ids = fields.One2many('cr.emp.pledge','promise_id',string='承诺书列表')


    @api.model
    def add_promise(self):
        staffs=self.env['cr.emp.staff'].search([])
        staff_ids=staffs.ids
        now = datetime.datetime.now()
        for staff_id in staff_ids:
            score=self.search([('staff_id','=',staff_id)])
            if not score:
                self.create({
                    'staff_id':staff_id,
                })

class CrEmpPledge(models.Model):

    _name = "cr.emp.pledge"
    _description = "承诺书"

    name = fields.Char(string='姓名',related='staff_id.name',store=True)
    card_id = fields.Char(string='工号',related='staff_id.card_id',store=True)
    ws_id = fields.Many2one('cr.emp.workshop', string='所属车间', related='staff_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team", string='班组', related='staff_id.team_id',store=True)
    post_id = fields.Many2one("cr.emp.post", string='岗位', related='staff_id.post_id',store=True)
    staff_id = fields.Many2one('cr.emp.staff',string='员工')
    promise_id = fields.Many2one('cr.emp.promise',string='承诺')
    year = fields.Char(string='年')
    month = fields.Char(string='月')
    detail = fields.Text(string='内容')

    @api.model
    def create(self, values):
        pledge=super(CrEmpPledge, self).create(values)
        promise=self.env['cr.emp.promise'].search([('staff_id','=',pledge.staff_id.id)])[0]
        pledge.write({'promise_id':promise.id})
        return pledge


